/**
 * @file dbus_common.h
 * @brief D-Bus测试程序公共定义
 */
#ifndef DBUS_COMMON_H
#define DBUS_COMMON_H

#include <dbus/dbus.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* D-Bus服务定义 */
#define TEST_SERVICE_NAME "com.hollyland.test"
#define TEST_OBJECT_PATH "/com/hollyland/test"
#define TEST_INTERFACE_NAME "com.hollyland.test.Interface"

/* 方法和信号定义 */
#define TEST_METHOD_NAME "TestMethod"
#define TEST_SIGNAL_NAME "TestSignal"

/* 错误处理宏 */
#define CHECK_ERROR(error)                                                            \
    do                                                                                \
    {                                                                                 \
        if (dbus_error_is_set(&(error)))                                              \
        {                                                                             \
            fprintf(stderr, "D-Bus Error: %s - %s\n", (error).name, (error).message); \
            dbus_error_free(&(error));                                                \
            return FALSE;                                                             \
        }                                                                             \
    } while (0)

#define CLEANUP_MESSAGE(msg)         \
    do                               \
    {                                \
        if (msg)                     \
        {                            \
            dbus_message_unref(msg); \
            msg = NULL;              \
        }                            \
    } while (0)

/**
 * @brief 方法处理器结构体
 */
typedef struct
{
    char interface[256];
    char member[128];
    DBusHandlerResult (*handler)(DBusConnection *connection, DBusMessage *message);
} MethodHandler;

/**
 * @brief 方法处理器注册表
 */
static MethodHandler g_method_handlers[32];
static int g_method_handler_count = 0;

/**
 * @brief 信号处理器结构体
 */
typedef struct
{
    char interface[256];
    char member[128];
    void (*handler)(DBusMessage *message);
} SignalHandler;

/**
 * @brief 信号处理器注册表
 */
static SignalHandler g_signal_handlers[32];
static int g_signal_handler_count = 0;
static char g_match_rules[32][512];
static int g_match_rule_count = 0;

#endif /* DBUS_COMMON_H */
